/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "ExportInfo.h"
#include "MMachineInfo.h"

#include "WorkDefinitions.h"

#include "MGlobalInfo.h"

#include "MPortDescriptionList.h"
#include "MDHCPInfoDesc.h"
#include "MNTPDecoder.h"
#include "MGxRPDecoder.h"
#include "MSTPDecoder.h"
#include "MCDPDecoder.h"
#include "MDTPDecoder.h"
#include "MWmiNames.h"

#include "MKOuiIabList.h"
#include "MKMcast4List.h"

class ExportXML
{	
private:
	CStdioFile outFile;

	CString RemoveStupidChars(CString);

	bool WriteLine(CString, int);

	bool AddTitle(CString);
	bool AddVersion(CString, CString);
	bool AddTimeStamp(CString);
	bool AddWorkCfgInfo(WorkCfg*);
	bool AddPortsForMethod(MMachineInfo*, WorkMethodType, MPortDescriptionList*);
	bool AddWmiInfoValue(CString, CString);

	WorkDefinitions wd;

	CString GetMethodName(WorkMethodType);

	bool AddNetBiosNames(MMachineInfo*);
	bool AddSharesInfo(MMachineInfo*);
	bool AddSnmpSysInfo(MMachineInfo*);
	bool AddDhcpInfo(MMachineInfo*);
	bool AddPromiscResults(MMachineInfo*);
	bool AddIgmpInfo(MMachineInfo*, MKMcast4List*);
	bool AddGVRPInfo(MMachineInfo*);
	bool AddGMRPInfo(MMachineInfo*);
	bool AddCDPInfo(MMachineInfo*);
	bool AddSTPInfo(MMachineInfo*);
	bool AddDTPInfo(MMachineInfo*);
	bool AddDnsNames(MMachineInfo*);

	bool AddWmiInfo(MWmiData*);
		bool AddWmiInfoOS(MWmi_Win32_OperatingSystem*);
		bool AddWmiInfoQFE(MWmi_Win32_QuickFixEngineering*);
			int GetWmiInfoQFECount(MWmi_Win32_QuickFixEngineering*);
		bool AddWmiInfoShares(MWmi_Win32_Share*);
		bool AddWmiInfoDiskDrive(MWmi_Win32_DiskDrive*);
		bool AddWmiInfoDiskPart(MWmi_Win32_DiskPartition*);
		bool AddWmiInfoProcessor(MWmi_Win32_Processor*);
		bool AddWmiInfoPhysicalMem(MWmi_Win32_PhysicalMemory*);
		bool AddWmiInfoCDROM(MWmi_Win32_CDROMDrive*);
		bool AddWmiInfoVideoController(MWmi_Win32_VideoController*);
		bool AddWmiInfoDesktopMonitor(MWmi_Win32_DesktopMonitor*);
		bool AddWmiInfoUserAccount(MWmi_Win32_UserAccount*);
			int GetWmiInfoUserAccountCount(MWmi_Win32_UserAccount*);
		bool AddWmiInfoGroup(MWmi_Win32_Group*);
			int GetWmiInfoGroupCount(MWmi_Win32_Group*);
		CString FormatTextString(CString);

	CString DwTimeToDHMS(DWORD);
	CString DwToIpStr(DWORD);

	MDHCPInfoDesc    infoDescDHCP;
	MNTPDecoder      mNTPDecoder;
	MGxRPDecoder     mGxRPDecoder;
	MSTPDecoder      mSTPDecoder;
	MCDPDecoder      mCDPDecoder;
	MDTPDecoder      mDTPDecoder;
	MWmiNames        wmiNames;

	int expHostId;

public:
	ExportXML(void);
	~ExportXML(void);

	bool BeginExport(ExportInfo*);
		bool ExportMachine(MMachineInfo*, MKOuiIabList*, MKMcast4List*, MPortDescriptionList*);
		bool ExportGlobalInfo(MGlobalInfo*);
	bool EndExport(void);
};

//---------------------------------------------------------------------------
// Model:
//---------------------------------------------------------------------------
/*
	<CHScannerDoc>

		<pageTitle>

		<version>
			<vApp>
			<vNumber>

		<timestamp>Thu Dec 27 15:02:39 2005</timestamp>

		<workCfg type=<active|passive|hunt>>
			<cFileName>
			<cDescription>
			<cComment>
			<cAddressSpace>
			<cScanAll>
			<cRandomize>
			<cMaxSimScanG>
			<cMaxSimScanU>
			<cMaxSimScanI>
			<cMaxWaitTime>
			<cJobs>
				<cJob>
					<cPhaseType>
					<cMethodType>
					<cMimic>
					<cPorts> || <cProtocols> || nimic

		<result id=<...>>
			<addrIP>
			<addrIPv6>
			<addrMAC type=<Arp|NetBios>>
			<addrIPX>
			<countryCode>
			<dnsNames>
				<dnsName>
					<dnsNameType>
					<dnsNameValue>
					<dnsNameTTL>
			<ssdp>
			<stopped>
			<netBios>
				<netBiosName>
				...
			<shares>
				<shareInfo>
				...
			<snmpSysInfo>
				<snmpSysInfoItem type=<...>>
				...
			<dhcpInfo>
					<dhcpInfoItem type=<yiAddr|chAddr|siAddr|giAddr>>
					...
					<dhcpInfoOption>
						<dhcpInfoOptionId>
						<dhcpInfoOptionName>
						<dhcpInfoOptionValue>
					...
			<promiscInfo>
				<promiscInfoItem type=<bCast47|bCast16|bCast8|GroupBit|mCast0|mCast1>
			<igmpInfo>
				<igmpInfoItem type=<...> [assignment=<...>] [recordType=<...>]>


			<gvrpInfo [name=<...>]>
				<gvrpInfoItem type =<...>>
			<gmrpInfo>
				<gmrpInfoItem type =<...>>

			<cdpInfo>
				<cdpInfoItem type=<...>>
			<stpInfo>
				<stpInfoItem type=<...>>
			<dtpInfo>
				<dtpInfoItem type=<...>>

			<methodName> (e.g.: SYN Scan)
				<methodResult> (e.g.: 80 (http))
			<methodName> (e.g.: SYN Scan)
				<methodResult> (e.g.: 80 (http))
			...
			<wmiInfo>
				<wmiInfoOS | wmiInfoQFE cnt=<...> | wmiInfoShare | wmiInfoProcessor | wmiInfoPhysicalMemory | 
					wmiInfoDiskDrive | wmiInfoDiskPartition | wmiInfoCDROMDrive | wmiInfoVideoController | 
					wmiInfoDesktopMonitor | wmiInfoUserAccount cnt=<...> | wmiInfoGroup cnt=<...>>
					<wmiInfoObject>
						<wmiInfoValue name=<...>>

		<result>
			<addrIP>
			<addrMAC type=<Arp|NetBios> [vendor=<...>]>
			<dnsName time>
			<method name> (e.g.: SYN Scan)
				<methodResult> (e.g.: 80 (http))
			<methodName> (e.g.: SYN Scan)
				<methodResult> (e.g.: 80 (http))
			...

		...

		<globalInfo>
			<giEthCnt>
				<gieCnt type=<Arp|Tcp4|Udp4|Icmp4|Other4|Tcp6|Udp6|Icmp6|Other6|Other|Total>>
			<giNTP>
				<gin type=<IP|MAC|Stratum|Poll|Precision|RootDelay|RootDisp|ReferenceTstamp|OriginateTstamp|ReceiveTstamp|TransmitTstamp>>
*/
